/**
 * Copyright 2007 American Well Systems
 * All rights reserved.
 *
 * It is illegal to use, reproduce or distribute
 * any part of this Intellectual Property without
 * prior written authorization from American Well.
 */
package org.rich.charlesmurphy.ui;

import static org.rich.charlesmurphy.core.CMIds.*;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.ddevil.data.BasicDisplayDescriptor;
import org.ddevil.data.DisplayDescriptor;

/**
 * @author Rich O'Connell
 */
public class LabelFactory {


	private static final Map<String, String> labelMap;
	static {
		labelMap = new HashMap<String, String>();

		labelMap.put(TIME, "Timetamp");
		labelMap.put(RESPONSE_SIZE, "Resp bytes");
		labelMap.put(OTHER, "Other");
		labelMap.put(IMAGES, "Images");
		labelMap.put(AJAX, "Ajax");
		labelMap.put(JS, "JS");
		labelMap.put(CSS, "CSS");
		labelMap.put(CACHE, "Cached");
		labelMap.put(SUCCESS, "Not-Cached");
		labelMap.put(REQUESTS, "Total");
		labelMap.put(SAMPLE_SIZE, "Samples");
		labelMap.put(URL, "URL");
		labelMap.put(EXCEPTION, "Exception");
		labelMap.put(MIME_TYPE, "Mime-type");
		labelMap.put(DOWNLOAD_SPEED, "DL Speed (KB/s)");
		labelMap.put(ELAPSED_TIME, "Time Taken (ms)");
		labelMap.put(RESPONSE_BODY_SIZE, "Resp Body (b)");
		labelMap.put(RESPONSE_HEADER_SIZE, "Resp Header (b)");
		labelMap.put(REQUEST_BODY_SIZE, "Req Body (b)");
		labelMap.put(REQUEST_HEADER_SIZE, "Req Headers (b)");
		labelMap.put(START_TIME, "Start Time");
		labelMap.put(STATUS, "Status");
		labelMap.put(METHOD, "Method");
		labelMap.put(QUERY, "Query String");
		labelMap.put(PATH, "URL Path");
		labelMap.put(TOTAL_PAGE_TIME, "Elapsed (sec)");
		labelMap.put(REMOTE_HOST, "R. Host");
		labelMap.put(AVERAGE_SIZE, "Avg Size (b)");
		labelMap.put(SOURCE_COUNT, "Sources");
		labelMap.put(DISTINCT_SOURCE_COUNT, "Distinct Sources");
		labelMap.put(MAX_GEN_REQUETS, "Max gen reqs");
		labelMap.put(PARAM_NAMES, "Param names");
	}

	private static String get(String id) {
		String rv = labelMap.get(id);
		if (rv == null) {
			int idx = id.lastIndexOf(".");
			if (idx > -1) {
				rv = labelMap.get(id.substring(idx + 1));
			}
		}
		if (rv == null) {
			throw new RuntimeException("No label for id: " + id);
		}
		return rv;
	}

	public static String getLabel(String id) {
		return get(id);
	}

	public static DisplayDescriptor makeDisplayDescriptor(String ...ids) {
		return makeDisplayDescriptor(Arrays.asList(ids));
	}

	public static DisplayDescriptor makeDisplayDescriptor(Iterable<String> ids) {
		DisplayDescriptor dd = new BasicDisplayDescriptor();
		for (String id : ids) {
			dd.setLabel(id, get(id));
		}
		return dd;
	}
	
	
	private static String dateFormat = "[M-d-yy] HH:mm:ss";
	public static String getDateFormat() {
		return dateFormat;
	}
	
	private static SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
	public static String formatDate(Date date) {
		return sdf.format(date);
	}

}
